package com.xiaotu.storage;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DataRedisKeyUtils;
import com.xiaotu.common.util.RegexUtils;
import com.xiaotu.storage.converter.AConverter;
import com.xiaotu.storage.converter.ConverterFactory;
import com.xiaotu.storage.model.CacheDataModel;

import net.sf.json.JSONObject;

/**
 * @类名 MovieBoxofficeStorageProcessor
 * @日期 2017年2月27日
 * @作者 高海军
 * @功能 票房数据转储实现类
 */
@Component("MovieBoxofficeStorageProcessor")
public class MovieBoxofficeStorageProcessor extends AHSetStorageProcessor
{
    private static final String[] VALIDATE_ARR = new String[]{"tvName", "boxoffice",
            "totalBoxoffice", "timestamp"};

    private static final String[] FIELD_ARR = new String[]{"tvName", "boxoffice",
            "totalBoxoffice", "sliceratio", "boxofficeRatio", "screen", "releaseDates",
            "playDate", "attendanceRatio", "avgNumber", "seat", "seatProportion", "siteName",
            "dataId", "timestamp"};

    private static final String[] CONVERT_ARR = new String[]{AConverter.STRING_CONVERT,
            AConverter.NUMBER_CONVERT, AConverter.NUMBER_CONVERT, AConverter.GET_NUMBER_CONVERT,
            AConverter.GET_NUMBER_CONVERT, AConverter.NUMBER_CONVERT, AConverter
            .GET_NUMBER_CONVERT, AConverter.STRING_CONVERT, AConverter.GET_NUMBER_CONVERT,
            AConverter.NUMBER_CONVERT, AConverter.NUMBER_CONVERT, AConverter.GET_NUMBER_CONVERT,
            AConverter.STRING_CONVERT, AConverter.STRING_CONVERT, AConverter.TIMESTAMP_CONVERT};

    private static final boolean[] THROW_ARR = new boolean[]{true, true, true, true, true, true,
            true, true, true, true, true, true, true, true, true, true, true, true, true};

    @Override
    protected String getRedisKey(Date date)
    {
        return DataRedisKeyUtils.getKey(Constants.DataRedisKey.MOVIE_BOXOFFICE_YYYYMMDD, date);
    }

    @Override
    public String process(JSONObject target, CacheDataModel cacheData,
            Date date)
    {
        this.validateAllNotEmpty(target, VALIDATE_ARR);
        this.setReleaseDays(target);
        return this.list2CSVLine(ConverterFactory.convert(target, FIELD_ARR, CONVERT_ARR,
                THROW_ARR));
    }

    /**
     * 设置上映天数
     *
     * @param target 目标对象
     */
    private void setReleaseDays(JSONObject target)
    {
        if (!target.has("releaseDates") || StringUtils.isEmpty(target.getString("releaseDates")))
            return;
        String days = target.getString("releaseDates");
        if ("上映首日".equals(days))
        {
            target.put("releaseDates", "1");
            return;
        }
        if (StringUtils.isEmpty(RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER, days)))
        {
            target.put("releaseDates", null);
            return;
        }
        if (StringUtils.isNotEmpty(RegexUtils.regexSearch(RegexUtils.REGEX_INTEGER + "{4}-" +
                RegexUtils.REGEX_INTEGER + "{2}", days)))
            target.put("releaseDates", null);
    }

    @Override
    public String getDataType()
    {
        return Constants.PageDataType.SEARCH_LIST;
    }

    @Override
    public String getTaskType()
    {
        return Constants.SpiderTaskType.MOV_BOXOFFICE;
    }
}
